﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCFReactiveX.Example.CodePlex;

namespace WCFReactiveX.Example
{
    public class CodePlexServiceController
    {
        private readonly RxController _rxController;

        public CodePlexServiceController()
        {
            this._rxController = new RxController();
            this._rxController.OnProviderRequest += new Action<IProviderTransaction>(_rxController_OnProviderRequest);
            this._rxController.OnProviderResponse += new Action<IProviderTransaction>(_rxController_OnProviderResponse);
        }

        void _rxController_OnProviderResponse(IProviderTransaction obj)
        {
            //Log or pass up during an event
        }

        void _rxController_OnProviderRequest(IProviderTransaction obj)
        {
            //Log or pass up during an event
        }

        public CodePlexRelease GetRelease(string projectName, string releaseName, string userName, string password)
        {
           
            //Setup the request context
            var context = new GetReleaseRequestContext(new CodePlexToken 
                                                        {
                                                            UserName = userName,
                                                            Password = password 
                                                        })
                                                            {
                                                                ProjectName = projectName,
                                                                ReleaseName = releaseName 
                                                            };

            CodePlexRelease release = null;
            var client = new ReleaseServiceSoapClient();

            //Execute the request, get results pushed async
            this._rxController.ExecuteRequestAsync<GetReleaseRequestContext, CodePlexToken, CodePlexMessage, CodePlexException, ReleaseServiceSoap>
                (context,
                (GetReleaseRequestContext c) =>
                {
                    if (string.IsNullOrEmpty(c.Token.UserName) || string.IsNullOrEmpty(c.Token.Password))
                        throw new Exception("Please provide a user name or password");
                },
                (GetReleaseRequestContext c) =>
                {
                    var result = this.MapObject<Release, CodePlexRelease>(
                        client.GetRelease(c.ProjectName, c.ReleaseName, c.Token.UserName, c.Token.Password));

                    //Create a message saying it was successful, this could be mapped to messages returned from the Web Service
                    //however codeplex does not return any messages
                    if(result != null)
                        result.Messages = new List<CodePlexMessage>() { new CodePlexMessage { Message = "Success" } };

                    return result;
                },
                (IProviderResult<CodePlexMessage> result) =>
                {
                    release = result as CodePlexRelease;
                },
                client);

            return release;
        }
    }

    
}
